package com.kzd.gtgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.jetpack.manager.GameRepository
import com.module.lemlin.http.*
import java.io.Serializable

class GameViewModel(application: Application) : BaseViewModel(application) {

    data class GameUpdate(
        val type: String,
        val isLoadMore: Boolean = false
    ):Serializable

    private val mGameRepository: GameRepository = GameRepository()

    private var page: Int = 1
    private var limit: Int = 10

    lateinit var mGameId: String

    val selectedClassifyIdLiveData = MutableLiveData<GameUpdate>()

    val gameDetailsLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<GameDetails>>>()
    fun gameDetails(gid: String) {
        mGameId = gid
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<GameDetails>>(gameDetailsLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<GameDetails>) {
                super.onSuccess(resp.apply { mGameId = data.id?:return@apply })
                gameGuess()
            }
        }) {
            mGameRepository.gameDetails(gid)
        }
    }

    val gameGuessLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun gameGuess() {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameBean>>(gameGuessLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBean>) {
                super.onSuccess(resp)
                this@GameViewModel.page += 1
            }
        }) {
            mGameRepository.gameGuess(page.toString(), mGameId)
        }
    }

    val gameDiscussLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameDiscuss>>>()
    fun gameDiscuss(gid: String, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameDiscuss>>(gameDiscussLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameDiscuss>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mGameRepository.gameDiscuss(gid, page.toString(), limit.toString())
        }
    }

    val discussChildLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameDiscuss>>>()
    fun gameDiscussReply(id: String, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameDiscuss>>(discussChildLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameDiscuss>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mGameRepository.gameDiscussReply(id, page.toString(), limit.toString())
        }
    }

    val discussGreatLiveData = MutableLiveData<HttpResponseBody<DiscussGreatResponse>>()
    fun gameDiscussGreat(id: String, isGreat: Int, position: Int = 0) {
        setRxLifeScope(object : SubscribeViewModel<DiscussGreatResponse>(discussGreatLiveData) {
            override fun onSuccess(resp: DiscussGreatResponse) {
                super.onSuccess(resp.apply {
                    this.isGreat = if (isGreat == 1) 0 else 1
                    this.position = position
                })
            }
        }) {
            mGameRepository.gameDiscussGreat(id)
        }
    }

    val gameWelfareLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<GameWelfare>>>()
    fun gameWelfare() {
        setRxLifeScope(SubscribeViewModel(gameWelfareLiveData)) {
            mGameRepository.gameWelfare(mGameId)
        }
    }

    val gameServiceLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameService1>>>()
    fun gameService(gid: String = mGameId, isLoadMore: Boolean = false, limit: Int = this.limit) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameService1>>(gameServiceLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameService1>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mGameRepository.gameService(gid, page.toString(), limit.toString())
        }
    }

    val serviceRemindLiveData = MutableLiveData<HttpResponseBody<ServiceRemindResponse>>()
    fun gameServiceRemind(id: String, isRemind: Int, position: Int) {
        setRxLifeScope(object : SubscribeViewModel<ServiceRemindResponse>(serviceRemindLiveData) {
            override fun onSuccess(resp: ServiceRemindResponse) {
                super.onSuccess(resp.apply {
                    this.isRemind = if (isRemind == 0) 1 else 0
                    this.position = position
                })
            }
        }) {
            mGameRepository.gameServiceRemind(id)
        }
    }


    val gameBargainLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBargain>>>()
    fun gameBargain(
        keyword: String = "", sort: Int = 0, system: Int = 3, isLoadMore: Boolean = false
    ) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameBargain>>(gameBargainLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBargain>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mGameRepository.gameBargain(
                keyword, mGameId,
                sort.toString(),
                system.toString(),
                page.toString(),
                limit.toString()
            )
        }

    }

    val gameClassifyLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<ClassifyTab>>>()
    fun gameClassify() {
        setRxLifeScope(SubscribeViewModel(gameClassifyLiveData)) {
            mGameRepository.gameClassify()
        }
    }

    val gameListLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun gameList(type: String, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameBean>>(gameListLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBean>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mGameRepository.gameList(type, page.toString())
        }
    }

    val gameFeaturedLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<GameFeatured>>>()
    fun gameFeatured(systemType: String = "1", ios: String = "0") {
        setRxLifeScope(SubscribeViewModel(gameFeaturedLiveData)) {
            mGameRepository.gameFeatured(systemType, ios)
        }
    }

    val gameRankingLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun gameRanking(type: String, cid: String = "0", isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameBean>>(gameRankingLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameBean>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mGameRepository.gameRanking(type, cid, page.toString())
        }
    }

}